﻿using System;
using System.Web.Mvc;
using Inscriptionem.Areas.Inscriptionem.Models.Enums;
using Inscriptionem.Areas.Inscriptionem.Models.Requests;
using Inscriptionem.Areas.Inscriptionem.Models.Views;
using Inscriptionem.Authorization;
using Inscriptionem.Extensions;

namespace Inscriptionem.Areas.Inscriptionem.Controllers
{
    [InscriptionemAuthorize(MemberRole.Admin)]
    public class SettingsController : Controller
    {
        public ActionResult Index()
        {
            var settings = MakeModel();

            var result = new SettingsViewModel();
            settings.CopyTo(result);

            return View("Index", result);
        }

        [HttpPost]
        public ActionResult Index(SettingsRequestModel model)
        {
            var result = new SettingsViewModel();
            model.CopyTo(result);

            result.Error = ValidateSettingsModel(result);

            if (result.Error == SettingsError.None)
            {
                // Save
                try
                {
                    var configuration = Configuration.Configuration.Instance;

                    configuration.General.URL = result.URL;
                    configuration.General.Title = result.Title;

                    configuration.Comments.Allowed = result.CommentsAllowed;
                    configuration.Comments.PublishApprovalRequired = result.CommentsPublishApprovalRequired;

                    configuration.Members.RegistrationAllowed = result.RegistrationAllowed;
                    configuration.Members.RegistrationApprovalRequired = result.RegistrationApprovalRequired;

                    configuration.Mail.Sender = result.Sender;
                    configuration.Mail.Server = result.Server;
                    configuration.Mail.Login = result.Login;
                    configuration.Mail.Port = result.Port;
                    configuration.Mail.SSL = result.SSL;

                    configuration.Blog.PageSize = result.PageSize;

                    if (!string.IsNullOrWhiteSpace(result.Password))
                    {
                        configuration.Mail.Password = result.Password;
                    }

                    result.Error = SettingsError.Saved;
                }
                catch (Exception)
                {
                    result.Error = SettingsError.Other;
                }
            }

            return View("Index", result);
        }

        private SettingsError ValidateSettingsModel(SettingsViewModel model)
        {
            if (string.IsNullOrWhiteSpace(model.URL))
            {
                return SettingsError.UrlEmpty;
            }
            
            if (!model.URL.ToLower().ContainsAny(new string[] { "http://", "https://" }) && model.URL.ToLower() != "http://" && model.URL.ToLower() != "https://")
            {
                return SettingsError.UrlInvalid;
            }
            
            if (!string.IsNullOrWhiteSpace(model.Sender) && !model.Sender.Contains("@"))
            {
                return SettingsError.MailInvalid;
            }
            
            if (!string.IsNullOrWhiteSpace(model.Password))
            {
                if (string.IsNullOrWhiteSpace(model.PasswordConfirmation))
                {
                    return SettingsError.PasswordEmpty;
                }

                if (model.Password != model.PasswordConfirmation)
                {
                    return SettingsError.PasswordNotMatched;
                }
            }

            if (model.PageSize <= 0)
            {
                return SettingsError.PageSizeInvalid;
            }

            return SettingsError.None;
        }

        private SettingsRequestModel MakeModel()
        {
            var result = new SettingsRequestModel();

            var configuration = Configuration.Configuration.Instance;

            result.URL = configuration.General.URL;
            result.Title = configuration.General.Title;

            result.CommentsAllowed = configuration.Comments.Allowed;
            result.CommentsPublishApprovalRequired = configuration.Comments.PublishApprovalRequired;

            result.RegistrationAllowed = configuration.Members.RegistrationAllowed;
            result.RegistrationApprovalRequired = configuration.Members.RegistrationApprovalRequired;

            result.Sender = configuration.Mail.Sender;
            result.Server = configuration.Mail.Server;
            result.Login = configuration.Mail.Login;
            result.Port = configuration.Mail.Port;
            result.SSL = configuration.Mail.SSL;

            result.PageSize = configuration.Blog.PageSize;

            return result;
        }
    }
}
